﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using DesktopGarden.Classes;
using DesktopGarden.Windows;

namespace DesktopGarden.Controls
{
    /// <summary>
    /// Interaction logic for FlowerTile.xaml
    /// </summary>
    public partial class FlowerTile : UserControl
    {
        public List<FlowerInstance> Flowers { get; set; }

        public EventHandler SelectedEvent;

        public EventHandler UnSelectedEvent;

        public EventHandler FlowerAdded;

        public EventHandler FlowerDeleted;


        public List<FlowerInstance> TempAddedFlowers { get; set; }

        public List<FlowerInstance> TempDeletedFlowers { get; set; }


        public int Row { get; set; }

        public int Column { get; set; }

        bool _selected = false;



        public bool Selected { 
            get{ 
                return _selected;
            }

            set{
                _selected = value;

                if (_selected)
                {
                   // border.BorderBrush = SELECTEDCOLOR;
                    if (SelectedEvent != null)
                        SelectedEvent(this, null);
                }
                else
                {
                    //border.BorderBrush = NOTSELECTEDCOLOR;
                    if (UnSelectedEvent != null)
                        UnSelectedEvent(this, null);
                }
               }
        }

         Brush SELECTEDCOLOR = Brushes.Red;

         Brush NOTSELECTEDCOLOR = Brushes.Black;

         Brush EMPTYTILE = Brushes.White;



        public FlowerTile(List<FlowerInstance> flowers)
        {
            InitializeComponent();
            this.Flowers = flowers;
            TempAddedFlowers = new List<FlowerInstance>();
            TempDeletedFlowers = new List<FlowerInstance>();

            LoadFlowerColors();
        }

        private void LoadFlowerColors()
        {
            border.Child = null;

            UniformGrid ug = MakeGrid();

            if (Flowers.Count == 0 && TempAddedFlowers.Count == 0)
                ug.Children.Add(new Grid() { Background = EMPTYTILE});

            LoadFlowers(Flowers, ug);
            LoadFlowers(TempAddedFlowers, ug);

           

            border.Child = ug;
        }


        private void LoadFlowers(List<FlowerInstance> flowers, UniformGrid ug)
        {
            foreach (FlowerInstance f in flowers)
            {
             
                    Grid g = new Grid() { Background = f.Color };
                    ug.Children.Add(g);
                    g.ToolTip = new MiniFlowerDisplay(f);
                    g.MouseUp += (x, y) =>
                    {
                        if (App.CurrentMode == App.Mode.Edit)
                        {
                            FlowerEditor viewFlower = new FlowerEditor(f);
                            if ((bool)viewFlower.ShowDialog())
                            {

                            }
                        }
                    };
                
            }
        }
        private void AddFlower(FlowerInstance flower)
        {

            if (!Flowers.Contains(flower) && !TempAddedFlowers.Contains(flower) && Flowers.Count < 10)
            {
                TempAddedFlowers.Add(flower);
                flower.AddPlantedFlower(Row, Column);
                LoadFlowerColors();
                if (FlowerAdded != null)
                    FlowerAdded(this, null);
            }
        }

        public void RemoveFlower(FlowerInstance flower)
        {
            if (Flowers.Contains(flower))
            {
                flower.RemovePlantedFlower(Row, Column);
                Flowers.Remove(flower);
                TempDeletedFlowers.Add(flower);
                LoadFlowerColors();
                if (FlowerDeleted != null)
                    FlowerDeleted(this, null);
            }
        }

        /// <summary>
        /// Determines how many flowers are in the tile, and makes a appopriate uniform grid to hold the colors.
        /// </summary>
        /// <returns>The Uniform Grid to place the flowers in.</returns>
        private UniformGrid MakeGrid()
        {
            int flowerCount = Flowers.Count + TempAddedFlowers.Count;

            UniformGrid ug = new UniformGrid();

            if (flowerCount == 1 || flowerCount == 0)
            {
                ug.Rows = 1;
                ug.Columns = 1;
            }
            else if (flowerCount == 2)
            {
                ug.Rows = 1;
                ug.Columns = 2;
            }
            else if (flowerCount < 5)
            {
                ug.Rows = 2;
                ug.Columns = 2;
            }
            else
            {
                ug.Rows = 3;
                ug.Columns = 3;
            }

            return ug;
        }

        private void MouseLeaveTile(object sender, MouseEventArgs e)
        {
            //if (!Selected)
                border.BorderBrush = NOTSELECTEDCOLOR;
        }

        private void MouseEnterTile(object sender, MouseEventArgs e)
        {
            border.BorderBrush = SELECTEDCOLOR;

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Click();
            }
        }

        private void MouseClickTile(object sender, MouseButtonEventArgs e)
        {
            Click();
        }

        private void Click()
        {
            Selected = !Selected;

            if (MainWindow.SelectedFlower != null)
            {
                if (App.CurrentMode == App.Mode.Plant)
                    AddFlower(MainWindow.SelectedFlower);
                else if (App.CurrentMode == App.Mode.Delete)
                    RemoveFlower(MainWindow.SelectedFlower);
            }
        }

        public void CancelAddedFlowers()
        {
            foreach (FlowerInstance f in TempAddedFlowers)
                f.CancelAddedFlowers();

            TempAddedFlowers.Clear();

            LoadFlowerColors();
        }

        public void SaveAddedFlowers()
        {
            foreach (FlowerInstance f in TempAddedFlowers)
            {
                Flowers.Add(f);
                f.SaveAddedFlowers();
            }
            TempAddedFlowers.Clear();

        }

        public void CancelDeletedFlowers()
        {
            foreach (FlowerInstance f in TempDeletedFlowers)
            {
                f.CancelDeletedFlowers();
                Flowers.Add(f);
            }

            TempDeletedFlowers.Clear();

            LoadFlowerColors();
        }


        public void SaveDeletedFlowers()
        {
            foreach (FlowerInstance f in TempDeletedFlowers)
                f.SaveDeletedFlowers();

            TempDeletedFlowers.Clear();
        }

        public void Clear()
        {
            Selected = false;
        }

        public bool HasFlower(FlowerInstance fl)
        {
            return Flowers.Contains(fl);
        }

        internal void Refresh()
        {
            LoadFlowerColors();
        }
    }
}
